package cn.iocoder.yudao.module.wine.service.supplyorderevaluate;

import cn.iocoder.yudao.framework.ip.core.utils.AreaUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.MPJLambdaWrapperX;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.wine.controller.app.supplyorderevaluate.enums.SupplyOrderEvaluateEnum;
import cn.iocoder.yudao.module.wine.dal.dataobject.businesscredit.BusinessCreditDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.businessuser.BusinessUserDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.membertype.MemberTypeDO;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorder.SupplyOrderDO;
import cn.iocoder.yudao.module.wine.dal.mysql.supplyorder.SupplyOrderMapper;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.wine.controller.app.supplyorderevaluate.vo.*;
import cn.iocoder.yudao.module.wine.dal.dataobject.supplyorderevaluate.SupplyOrderEvaluateDO;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.wine.dal.mysql.supplyorderevaluate.SupplyOrderEvaluateMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wine.enums.ErrorCodeConstants.*;

/**
 * 订单评价 Service 实现类
 *
 * @author 管理员
 */
@Service
@Validated
public class SupplyOrderEvaluateServiceImpl implements SupplyOrderEvaluateService {

    @Resource
    private SupplyOrderEvaluateMapper supplyOrderEvaluateMapper;

    @Resource
    private SupplyOrderMapper supplyOrderMapper;

    @Override
    public Long createSupplyOrderEvaluate(@NotNull AppSupplyOrderEvaluateSaveReqVO createReqVO) {
        Long orderId = createReqVO.getOrderId();
        SupplyOrderDO supplyOrderDO = supplyOrderMapper.selectOne(new LambdaQueryWrapperX<SupplyOrderDO>().eq(SupplyOrderDO::getId, orderId));
        if (supplyOrderDO == null) {
            throw exception(ORDER_INFO_NOT_EXISTS);
        }
        Long loginUserId = SecurityFrameworkUtils.getLoginUserId();
        Long buyUserId = supplyOrderDO.getBuyUserId();
        Long saleUserId = supplyOrderDO.getSaleUserId();
        /*
        修改成可评价
        if (!buyUserId.equals(loginUserId) && saleUserId.equals(loginUserId)){
          throw exception(NO_PERMISSION_EVALUATE);
        }
        */
        if (!(buyUserId.equals(loginUserId) || saleUserId.equals(loginUserId))){
          throw exception(NO_PERMISSION_EVALUATE);
        }
        //不能重复评价
        Long count = supplyOrderEvaluateMapper.selectCount(new LambdaQueryWrapperX<SupplyOrderEvaluateDO>()
                .eqIfPresent(SupplyOrderEvaluateDO::getFromUserId, loginUserId)
                .eqIfPresent(SupplyOrderEvaluateDO::getOrderId, orderId));
        if (count != 0L){
            throw exception(REPEAT_ADDITION);
        }

        //设置发布人id
        createReqVO.setFromUserId(loginUserId);
        createReqVO.setToUserId(buyUserId.equals(loginUserId)?saleUserId:buyUserId);
        // 插入
        SupplyOrderEvaluateDO supplyOrderEvaluate = BeanUtils.toBean(createReqVO, SupplyOrderEvaluateDO.class);
        supplyOrderEvaluateMapper.insert(supplyOrderEvaluate);
        // 返回
        return supplyOrderEvaluate.getId();
    }

    @Override
    public void updateSupplyOrderEvaluate(AppSupplyOrderEvaluateSaveReqVO updateReqVO) {
        // 校验存在
        validateSupplyOrderEvaluateExists(updateReqVO.getId());
        // 更新
        SupplyOrderEvaluateDO updateObj = BeanUtils.toBean(updateReqVO, SupplyOrderEvaluateDO.class);
        supplyOrderEvaluateMapper.updateById(updateObj);
    }

    @Override
    public void deleteSupplyOrderEvaluate(Long id) {
        // 校验存在
        validateSupplyOrderEvaluateExists(id);
        // 删除
        supplyOrderEvaluateMapper.deleteById(id);
    }

    private void validateSupplyOrderEvaluateExists(Long id) {
        if (supplyOrderEvaluateMapper.selectById(id) == null) {
            throw exception(INFO_NOT_EXISTS);
        }
    }

    @Override
    public AppSupplyOrderEvaluateRespVO getSupplyOrderEvaluate(Long id) {
        AppSupplyOrderEvaluateRespVO item = supplyOrderEvaluateMapper.getSupplyOrderEvaluate(id);
        if (item.getSaleUserId().equals(item.getFromUserId())) {
            item.setIdentity(SupplyOrderEvaluateEnum.SELLER.getContent());
        } else {
            item.setIdentity(SupplyOrderEvaluateEnum.BUYER.getContent());
        }
        return item;
    }


    @Override
    public List<AppSupplyOrderEvaluateRespVO> getSupplyOrderEvaluateList(AppSupplyOrderEvaluateListReqVO listReqVO) {
        List<AppSupplyOrderEvaluateRespVO> list = supplyOrderEvaluateMapper.selectSupplyOrderEvaluateList(listReqVO);
        if (CollectionUtils.isEmpty(list)){
            return list;
        }
        List<AppSupplyOrderEvaluateRespVO> collect = list.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId(), " ");
            item.setAreaName(format);
            if (item.getSaleUserId().equals(item.getFromUserId())) {
                item.setIdentity(SupplyOrderEvaluateEnum.SELLER.getContent());
            } else {
                item.setIdentity(SupplyOrderEvaluateEnum.BUYER.getContent());
            }
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<AppSupplyOrderEvaluateRespVO> getUserEvaluateList(Long userId) {
        List<AppSupplyOrderEvaluateRespVO> appSupplyOrderEvaluateRespVOS = supplyOrderEvaluateMapper.selectJoinList(AppSupplyOrderEvaluateRespVO.class, new MPJLambdaWrapperX<SupplyOrderEvaluateDO>()
                .eq(SupplyOrderEvaluateDO::getToUserId, userId)
                .isNotNull(SupplyOrderEvaluateDO::getOrderId)
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(SupplyOrderDO.class, SupplyOrderDO::getId, SupplyOrderEvaluateDO::getOrderId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyOrderEvaluateDO.class)
                .selectAs(BusinessCreditDO::getIcon, AppSupplyOrderEvaluateRespVO::getIcon)
                .selectAs(BusinessUserDO::getAreaId, AppSupplyOrderEvaluateRespVO::getAreaId)
                .selectAs(MemberUserDO::getNickname, AppSupplyOrderEvaluateRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, AppSupplyOrderEvaluateRespVO::getAvatar)
                .selectAs(MemberTypeDO::getName, AppSupplyOrderEvaluateRespVO::getMemberTypeName)
                .selectAs(MemberTypeDO::getIcon, AppSupplyOrderEvaluateRespVO::getMemberTypeIcon)
                .selectAs(MemberUserDO::getMark, AppSupplyOrderEvaluateRespVO::getMark)
                .selectAs(SupplyOrderDO::getSaleUserId, AppSupplyOrderEvaluateRespVO::getSaleUserId)
                .orderByDesc(SupplyOrderEvaluateDO::getCreateTime)
        );
        if (CollectionUtils.isEmpty(appSupplyOrderEvaluateRespVOS)){
            return appSupplyOrderEvaluateRespVOS;
        }
        List<AppSupplyOrderEvaluateRespVO> collect = appSupplyOrderEvaluateRespVOS.stream().peek(item -> {
            String format = AreaUtils.format(item.getAreaId(), " ");
            item.setAreaName(format);
            if (item.getSaleUserId().equals(item.getFromUserId())) {
                item.setIdentity(SupplyOrderEvaluateEnum.SELLER.getContent());
            } else {
                item.setIdentity(SupplyOrderEvaluateEnum.BUYER.getContent());
            }
        }).collect(Collectors.toList());
        return collect;

    }

    @Override
    public List<AppSupplyOrderEvaluateRespVO> getSupplyInfoEvaluateList(Long infoId) {

        List<AppSupplyOrderEvaluateRespVO> appSupplyOrderEvaluateRespVOS = supplyOrderEvaluateMapper.selectJoinList(AppSupplyOrderEvaluateRespVO.class, new MPJLambdaWrapperX<SupplyOrderEvaluateDO>()
                .eq(SupplyOrderEvaluateDO::getSupplyInfoId, infoId)
                .leftJoin(MemberUserDO.class, MemberUserDO::getId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(BusinessUserDO.class, BusinessUserDO::getUserId, SupplyOrderEvaluateDO::getFromUserId)
                .leftJoin(BusinessCreditDO.class, BusinessCreditDO::getId, BusinessUserDO::getCreditId)
                .leftJoin(MemberTypeDO.class, MemberTypeDO::getId, BusinessUserDO::getMemberTypeId)
                .selectAll(SupplyOrderEvaluateDO.class)
                .selectAs(BusinessCreditDO::getIcon, AppSupplyOrderEvaluateRespVO::getIcon)
                .selectAs(BusinessUserDO::getAreaId, AppSupplyOrderEvaluateRespVO::getAreaId)
                .selectAs(MemberUserDO::getNickname, AppSupplyOrderEvaluateRespVO::getNickname)
                .selectAs(MemberUserDO::getAvatar, AppSupplyOrderEvaluateRespVO::getAvatar)
                .selectAs(MemberTypeDO::getName, AppSupplyOrderEvaluateRespVO::getMemberTypeName)
                .selectAs(MemberTypeDO::getIcon, AppSupplyOrderEvaluateRespVO::getMemberTypeIcon)
                .selectAs(MemberUserDO::getMark, AppSupplyOrderEvaluateRespVO::getMark)
                .orderByDesc(SupplyOrderEvaluateDO::getCreateTime)
        );
        if (CollectionUtils.isEmpty(appSupplyOrderEvaluateRespVOS)){
            return appSupplyOrderEvaluateRespVOS;
        }
        return appSupplyOrderEvaluateRespVOS;
    }

}
